package com.dog.view.controller;

import com.dog.model.BaseEntity;
import com.dog.service.IBaseService;
import com.dog.view.beans.BaseBean;
import com.dog.view.model.BaseExtendedDataModel;
import org.apache.log4j.Logger;

import javax.faces.component.UIData;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;
import java.util.LinkedList;
import java.util.List;

/**
 * User: Kuttubek Aidaraliev
 * Date: 17.05.2010
 * Time: 9:26:43
 */
public abstract class BaseBeanController<Service extends IBaseService,
        Entity extends BaseEntity, EntityBean extends BaseBean> {

    private BaseExtendedDataModel baseExtendedDataModel;
    private Service service;
    private UIData dataTable;
    protected Logger logger = Logger.getRootLogger();

    protected BaseBeanController() {
    }


    public String preInsert(){
        setNewEntity(true);
        setEditField(true);
        setEntity(newEntity());
        return form();
    }

    public String preUpdate(){
        setNewEntity(false);
        setEditField(true);
        return form();
    }

    public String inserted(){
        setEntity((Entity)service.save(getEntity()));
        setNewEntity(false);
        setEditField(false);
        return null;
    }

    public String updated(){
        service.update(getEntity());
        setEditField(false);
        return view();
    }

    public String deleted(){
        service.delete(getEntity());
        setEntity(null);
        return list();
    }

    public String view(){
        return form();
    }

    public List<Entity> getRealList(){
        return service.getAll();
    }

    public List<SelectItem> getSelectList(){
        List<Entity> customers = service.getAll();
        List<SelectItem> selectItems = new LinkedList<SelectItem>();
        for (Entity entity : customers){
            selectItems.add(createSelectItem(entity));
        }

        return selectItems;
    }

    protected abstract String list();

    protected abstract String form();

    protected abstract Entity newEntity();

    protected abstract SelectItem createSelectItem(Entity entity);

    protected abstract String getNameEntity();

    protected abstract EntityBean createEntityBean();


    public BaseExtendedDataModel getBaseExtendedDataModel() {
        return baseExtendedDataModel;
    }

    public void setBaseExtendedDataModel(BaseExtendedDataModel baseExtendedDataModel) {
        this.baseExtendedDataModel = baseExtendedDataModel;
    }

    public BaseEntity getEntity() {
        return getEntityBean().getEntity();
    }

    public void setEntity(Entity entity){
        EntityBean entityBean = getEntityBean();
        entityBean.setEntity(entity);
        setEntityBean(entityBean);
    }

    public EntityBean getEntityBean(){
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
        EntityBean entityBean = (EntityBean)session.getAttribute(getNameEntity());

        if(entityBean == null) {
            entityBean = createEntityBean();
            setEntityBean(entityBean);
        }

        return entityBean;
    }

    public void setEntityBean(EntityBean entityBean){
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
        session.setAttribute(getNameEntity(), entityBean);
    }

    public Service getService() {
        return service;
    }

    public void setService(Service service) {
        this.service = service;
    }

    public boolean isNewEntity() {
        return getEntityBean().isNewEntity();
    }

    public void setNewEntity(boolean newEntity) {
        getEntityBean().setNewEntity(newEntity);
    }

    public UIData getDataTable() {
        return dataTable;
    }

    public void setDataTable(UIData dataTable) {
        this.dataTable = dataTable;
    }

    public boolean isEditField() {
        return getEntityBean().isEditField();
    }

    public void setEditField(boolean editField) {
        getEntityBean().setEditField(editField);
    }
    
}
